package com.android.people;

import java.io.File;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class PhotoWidgetConfig extends Activity {
    private static final String TAG = "PhotoWidgetConfig";
    private static final boolean DEBUG = false;
    
    public static final String BROWSE_RESULT = "browse_result";

    private Button mOkButton;
    private Button mCancelButton;
    private EditText mEditText;
    private Button mBrowseButton;

    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    private int mAppWidgetId = -1;
    private Context mContext;
    private Spinner mTimeSpinner;
    private int mUpdateTime = 1;
    private Toast mToast;

    private String[] mTimeLists = { "None", "1 minute", "2 minutes", "3 minutes", "5 minutes" };

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.setContentView(R.layout.config);

        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        mEditor = mSharedPreferences.edit();
        String previewpath = mSharedPreferences.getString(SlideImageViewService.PREVIEW_path, "");
        mUpdateTime = mSharedPreferences.getInt(SlideImageViewService.UPDATE_TIME, 1);

        mOkButton = (Button) this.findViewById(R.id.ok);
        mCancelButton = (Button) this.findViewById(R.id.cancel);
        mEditText = (EditText) this.findViewById(R.id.entry);
        mTimeSpinner = (Spinner) this.findViewById(R.id.time_spinner);
        mBrowseButton = (Button) this.findViewById(R.id.browser);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.time,
                                                                             android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mTimeSpinner.setAdapter(adapter);
        mTimeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                case 0:
                    mUpdateTime = 0;
                    break;
                case 1:
                    mUpdateTime = 1;
                    break;
                case 2:
                    mUpdateTime = 2;
                    break;
                case 3:
                    mUpdateTime = 3;
                    break;
                case 4:
                    mUpdateTime = 5;
                    break;
                case 5:
                    mUpdateTime = 10;
                    break;
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mBrowseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intentBrowse = new Intent(mContext, SDExplorer.class);
//                intentBrowse.setAction(Intent.ACTION_MAIN);
                startActivityForResult(intentBrowse, 0);
            }
        });
        
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        LOGD("The mAppWidgetId = " + mAppWidgetId);

        mContext = this;

        mCancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_CANCELED, resultValue);
                finish();
            }
        });

        mOkButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String path = mEditText.getText().toString();
                File file = new File(path);
                if (file.isDirectory() == false || file.exists() == false) {
//                    LOGD("The dir is not exists");
                    showTips(mContext.getString(R.string.tips_dir_error));
                    return;
                }
                if (path.endsWith("/") == false) {
                    path = path + "/";
                }
                mEditor.putString(SlideImageViewService.PREVIEW_path, path);
                mEditor.putInt(SlideImageViewService.UPDATE_TIME, mUpdateTime);
                mEditor.commit();

                LOGD("$$$$$$ the client info = " + path + "  " + mUpdateTime + "  " + mAppWidgetId);
                Intent intent = new Intent(mContext, SlideImageViewService.class);
                intent.putExtra(SlideImageViewService.WIDGET_ID, mAppWidgetId);
                intent.putExtra(SlideImageViewService.PREVIEW_path, path);
                intent.putExtra(SlideImageViewService.UPDATE_TIME, mUpdateTime);
                mContext.startService(intent);

                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
            }
        });
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            String dir = data.getStringExtra(BROWSE_RESULT);
            if (dir.equals("") == false) {
                mEditText.setText(dir);
            }
        }
    }
    
    private void showTips(CharSequence text) {
        showTips(text, Toast.LENGTH_SHORT);
    }
    
    private void showTips(CharSequence text, int duration) {
        if (mToast != null) {
            mToast.cancel();
            mToast.setDuration(duration);
            mToast.setText(text);
            mToast.show();
        } else {
            mToast = Toast.makeText(this, text, duration);
            mToast.show();
        }
    }

    public static void LOGD(String msg) {
        if (DEBUG) {
            Log.d(TAG, msg);
        }
    }
}
